import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
} from 'typeorm';

@Entity({ name: 'resource_files' })
export class ResourceFile {
  constructor(
    file?: Pick<
      Express.Multer.File,
      'originalname' | 'mimetype' | 'size' | 'filename' | 'path'
    >,
  ) {
    if (typeof file !== 'undefined') {
      this.originalname = file.originalname;
      this.mimetype = file.mimetype;
      this.size = file.size;
      this.filename = file.filename;
      this.path = file.path;
    }
  }

  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'character varying', length: 260 })
  originalname: string;

  @Column({ type: 'character varying', length: 150 })
  mimetype: string;

  @Column({ type: 'int' })
  size: number; // Byte

  @Column({ type: 'character varying', length: 50 })
  filename: string;

  @Column({ type: 'character varying', length: 100 })
  path: string;

  @Index({ unique: true })
  @Column({ type: 'character', length: 64, unique: true })
  sha256: string;

  @CreateDateColumn()
  uploadedAt: Date;
}
